/**
 * 数据字典设置
 * @return {}
 */
var dataDictionaryTree = function() {
	var dataDictionaryTreePanel = new Ext.tree.TreePanel({
				id : 'dataDictionaryTreePanel',
				region : 'west',
				width : 350,
				autoScroll : true,
				enableDD : false,// 是否支持拖拽效果
				loader : new Ext.tree.TreeLoader({
							dataUrl : __ctxPath
									+ "/systemDataDictionary/selectDictionaryTreeMenu"
						}),
				rootVisible : false,
				root : new Ext.tree.AsyncTreeNode({
							draggable : false,
							leaf : false,
							expand : true
						}),
				tbar : [{
							xtype : "button",
							text : "展开",
							iconCls : "btn-expand",
							scope : this,
							handler : function() {
								dataDictionaryTreePanel.expandAll();
							}
						}, {
							xtype : "tbseparator"
						}, {
							xtype : "button",
							text : "收起",
							iconCls : "btn-collapse",
							scope : this,
							handler : function() {
								dataDictionaryTreePanel.collapseAll();
							}
						}, {
							xtype : "tbseparator"
						}, "->", {
							id : "refresh",
							xtype : "button",
							text : "刷新",
							iconCls : "btn-refresh",
							handler : function() {
								dataDictionaryTreePanel.getRootNode().reload();
							}
						}]
			});
	// dataDictionaryTreePanel.expandAll();
	dataDictionaryTreePanel.on('rightClickCont',
			dataDictionaryTreePanel.rightClick, dataDictionaryTreePanel);

	var rightClick = new Ext.menu.Menu({
		items : [{
					text : '新增子节点',
					iconCls : "btn-add",
					handler : function() {
						var selectModel = dataDictionaryTreePanel
								.getSelectionModel();
						var node = selectModel.getSelectedNode();
						var rank = parseInt(node.attributes.url) + 1;

						Ext.getCmp('saveDataDictionary').enable();
						Ext.getCmp('dataDictionaryForm').getForm().reset();

						Ext.getCmp('dataDictionary_parentId').setValue(node.id);
						Ext.getCmp('dataDictionary_parentId_hidden')
								.setValue(node.id);
						Ext.getCmp("flag").setValue("insert");
					}
				}]
	});

	// 添加点击事件
	dataDictionaryTreePanel.on('click', function(node) {
				if (node.id != 0) {
					Ext.getCmp('saveDataDictionary').enable();
					Ext.getCmp('dataDictionaryForm').load({
						url : __ctxPath + "/systemDataDictionary/selectDataDictionaryMsgById",
						params : {
							id : node.id
						},
						root : 'data',
						success : function(form, action) {
							var jsonData = action.result.data;
							Ext.getCmp('dataDictionary_id_hidden')
									.setValue(jsonData.id);
							Ext.getCmp("flag").setValue("update");

						},
						failure : function(form, action) {
							Ext.Msg.alert("提示", "数据加载失败");
						}
					});
				}
			});

	// 增加右键弹出事件
	dataDictionaryTreePanel.on('contextmenu', function(node, event) {// 声明菜单类型
				event.preventDefault();// 这行是必须的，使用preventDefault方法可防止浏览器的默认事件操作发生。
				node.select();
				rightClick.showAt(event.getXY());// 取得鼠标点击坐标，展示菜单
			});

	return dataDictionaryTreePanel;
}

var updatedataDictionary = function() {

	var dataDictionaryBaseInfoField = new Ext.form.FieldSet({
				collapsible : false,
				border : false,
				buttonAlign : 'center',
				layout : 'form',
				items : [{
							xtype : 'textfield',
							name : 'keyes',
							anchor : '100%',
							allowBlank : false,
							blankText : '不能为空...',
							fieldLabel : '数据字典KEY'
						}, {
							xtype : 'textfield',
							name : 'value',
							anchor : '100%',
							fieldLabel : '数据字典VALUE'
						}, {
							xtype : 'numberfield',
							name : 'listOrder',
							anchor : '100%',
							allowBlank : false,
							blankText : '不能为空...',
							allowNegative : false,// 不允许输入负数
							allowDecimals : false,
							fieldLabel : '排序'
						}, {
							xtype : 'textfield',
							name : 'description',
							anchor : '100%',
							fieldLabel : '描述'
						}, {
							xtype : 'compositefield',
							fieldLabel : '是否可用',
							items : [{
										xtype : 'radio',
										boxLabel : '是',
										inputValue : "1",
										name : "isDisabled",
										checked : true
									}, {
										xtype : 'radio',
										boxLabel : '否',
										name : "isDisabled",
										inputValue : "0"
									}]
						}, {
							xtype : 'textfield',
							fieldLabel : '父编号',
							anchor : '100%',
							disabled : true,
							name:'parentId',
							id : 'dataDictionary_parentId'
						}, {
							xtype : 'textfield',
							fieldLabel : '编号',
							anchor : '100%',
							disabled : true,
							name : 'id'
						}, {
							xtype : 'textfield',
							fieldLabel : '创建人',
							anchor : '100%',
							disabled : true,
							name : 'createdMan'
						}, {
							xtype : 'textfield',
							fieldLabel : '创建时间',
							anchor : '100%',
							disabled : true,
							name : 'createdAt'
						}, {
							xtype : 'textfield',
							fieldLabel : '修改人',
							anchor : '100%',
							disabled : true,
							name : 'updatedMan'
						}, {
							xtype : 'textfield',
							fieldLabel : '修改时间',
							anchor : '100%',
							disabled : true,
							name : 'updatedAt'
						}, {
							xtype : 'textfield',
							hidden : true,
							id : 'dataDictionary_id_hidden',
							name : 'id'
						}, {
							id : 'dataDictionary_parentId_hidden',
							xtype : 'textfield',
							name : 'parentId',
							hidden : true
						}, {
							xtype : 'textfield',
							hidden : true,
							value : '',
							id : 'flag'
						}]
			});

	var centerPanel = new Ext.form.FormPanel({
		title : '数据字典信息',
		id : 'dataDictionaryForm',
		region : 'center',
		frame : true,
		buttonAlign : 'center',
		labelAlign : 'right',
		border : false,
		reader : new Ext.data.JsonReader({
					success : 'success',
					root : 'data'
				}, [{
							name : 'id'
						}, {
							name : 'parentId'
						}, {
							name : 'keyes'
						}, {
							name : 'value'
						}, {
							name : 'description'
						}, {
							name : 'isDisabled'
						}, {
							name : 'listOrder'
						}, {
							name : 'createdMan'
						}, {
							name : 'createdAt'
						}, {
							name : 'updatedMan'
						}, {
							name : 'updatedAt'
						}]),
		items : [dataDictionaryBaseInfoField],
		buttons : [{
					text : '保存',
					id : 'saveDataDictionary',
					disabled : true,
					handler : function() {
						var flag = Ext.getCmp("flag").getValue();;
						saveDataDictionary(flag);
					}
				}]
	});
	return centerPanel;
}

var dataDictionaryPanel = function() {
	return new Ext.Panel({
				anchor : '100% 100%',
				border : false,
				layout : 'border',
				items : [dataDictionaryTree(), updatedataDictionary()]
			});
}

var saveDataDictionary = function(flag) {
	var form = Ext.getCmp("dataDictionaryForm");
	if (form.getForm().isValid()) {
		var url = "";
		if (flag == 'insert') {
			url = __ctxPath + "/systemDataDictionary/insetDataDictionary";
		}
		if (flag == 'update') {
			url = __ctxPath + "/systemDataDictionary/updateDataDictionaryById";
		}
		Ext.Msg.confirm("信息确认", "您确认要保存吗？", function(c) {
					if (c == "yes") {
						form.getForm().submit({
							url : url,
							method : "POST",
							success : function(form, action) {
								Ext.MessageBox.show({
											title : "信息提示",
											msg : "保存成功!",
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								Ext.getCmp('dataDictionaryTreePanel')
										.getRootNode().reload();
								Ext.getCmp("flag").setValue("update");
							},
							failure : function(c, d) {
								Ext.MessageBox.show({
											title : "操作信息",
											msg : "保存失败，请重新保存!",
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							}
						});
					}
				});
	}
};

var dictionaryAdminResult = function(dictionaryId, list) {
	var dictionaryAdminSm = new Ext.grid.CheckboxSelectionModel();
	var dictionaryAdminCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), dictionaryAdminSm, {
				header : '人员账号',
				dataIndex : 'id',
				sortable : true,
				width : 120
			}, {
				header : '用户名',
				dataIndex : 'name',
				width : 100
			}, {
				header : '昵称',
				dataIndex : 'trueName',
				width : 120
			}, {
				header : '所属部门',
				dataIndex : 'deptName',
				width : 120
			}, {
				header : '角色编号',
				dataIndex : 'roleId',
				hidden : true,
				width : 120
			}, {
				header : '角色名称',
				dataIndex : 'roleName',
				hidden : true,
				width : 120
			}]);

	var dictionaryAdminRd = new Ext.data.JsonReader({// 表格的数据填充
		totalProperty : "totalCount",
		root : "list",
		fields : [{
					name : "id"
				}, {
					name : "name"
				}, {
					name : "trueName"
				}, {
					name : "deptName"
				}, {
					name : "roleId"
				}, {
					name : "roleName"
				}]
	});

	var dictionaryAdminDs = new Ext.data.GroupingStore({// 取数据
		proxy : new Ext.data.HttpProxy({
					url : __ctxPath + "/system/QueryFunctionRights.action"
				}),
		groupField : 'roleId',
		groupDir : 'ASC',
		remoteSort : false,
		reader : dictionaryAdminRd
	});

	dictionaryAdminDs.load();

	dictionaryAdminDs.on('load', function(store, records, options) {
				var arr = [];
				for (var i = 0; i < records.length; i++) {
					var record = records[i];
					for (var j = 0; j < list.length; j++) {
						if (list[j].id == record.get('id')) {
							arr.push(record);
						}
					}
				}
				dictionaryAdminSm.selectRecords(arr);
			}, this, {
				delay : 1000
			});

	var querydictionaryAdminTb = new Ext.Toolbar({// 定义grid的toolbar
		id : "querydictionaryAdmin",
		height : 25,
		items : []
	});
	querydictionaryAdminTb.add(new Ext.Button({
				text : "保存",
				width : 80,
				pressed : true,
				listeners : {
					"click" : function() {
						saveDictionaryAdmin(dictionaryId);
					}
				}
			}));
	var querydictionaryAdminGrid = new Ext.grid.GridPanel({
				cm : dictionaryAdminCm,
				sm : dictionaryAdminSm,
				id : "querydictionaryAdminGrid",
				store : dictionaryAdminDs,
				autoScroll : true,
				stripeRows : true,
				columnLines : true,
				loadMask : true,
				tbar : querydictionaryAdminTb,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				view : new Ext.grid.GroupingView({
							enableGroupingMenu : false,
							forceFit : true,
							startCollapsed : false,
							enableGrouping : true,
							groupTextTpl : '角色名称'
									+ '&nbsp;&nbsp;{[values.rs[0].data["roleName"]]}'
						}),
				viewConfig : {
					emptyText : '没有您想要的数据'
				},
				stateful : true,
				stateId : 'dictionaryAdminCookie',
				anchor : '100% 15%',
				frame : true
			});

	var dictionaryAdminWindow = new Ext.Window({
				width : 800,
				height : 550,
				plain : true,
				closeAction : 'close',
				modal : true,
				layout : 'fit',
				items : [querydictionaryAdminGrid]
			});

	return dictionaryAdminWindow;
};

function saveDictionaryAdmin(dictionaryId) {
	var e = Array();
	var b = Ext.getCmp("querydictionaryAdminGrid").getSelectionModel().getSelections();
	for (var i = 0; i < b.length; i++) {
		e.push(b[i].data.id);
	}
	Ext.Ajax.request({
				url : __ctxPath + "/system/GrantDictionaryAdminIds.action",
				method : "POST",
				params : {
					dictionaryId : dictionaryId,
					adminIds : e.toString()
				},
				success : function(f, g) {
					Ext.MessageBox.alert("操作信息", "操作成功！");
				},
				failure : function(f, g) {
					Ext.MessageBox.alert("操作信息", "操作失败！");
				},
				scope : this
			});
};

Ext.getCmp("centerTabPanel").getActiveTab().add(dataDictionaryPanel());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();